<?php

namespace app\company\controller\iot\setting;

use app\company\service\iot\setting\SettingIot;
use app\BaseController;
use app\common\Message;
use think\App;
use think\response\Json;

class Setting extends BaseController
{
    use Message;

    private SettingIot $settingIotModel;
    private mixed $data;
    private mixed $msg;//构造函数返回的消息

    public function __construct(App $app)
    {
        parent::__construct($app);
        $data = $this->request->param();
        $data['uid'] = $this->userData['id'];
        $data['areas_id'] = $this->userData['areas_id'];
        $data['unit_id'] = $this->userData['unit_id'];
        //只有公司层级可以创建，其他省市区的只能查看使用
        $message = '';
        $this->userData['type'] = 1;
        switch ($this->userData['type']){
            case '1':
                $data['company_id'] = $this->userData['company_id'];
                break;
            default:
                $data['company_id'] = -1;
                $message = '暂无权限使用此功能!';
                break;
        }
        $this->msg = $message;
        $this->settingIotModel = new SettingIot();
        $this->data = $data;
    }

    /**
     * 新增功能室分组
     * @return bool|string|Json
     */
    public function addGrouping(): Json|bool|string
    {
        try {
            if($this->data['company_id'] < 1) return error($this->msg);
            if(!$this->settingIotModel->addGrouping($this->data)) throw new \Exception($this->settingIotModel->getMessage());
            return success($this->settingIotModel->getResult(),$this->settingIotModel->getMessage());
        } catch (\Exception $e) {
            return error($e->getMessage(),$e->getCode(),[
                'line' => $e->getLine(),
                'file' => $e->getFile()
            ]);
        }
    }

    /**
     * 批量设置显示或禁止（支持批量设置）
     * @return bool|string|Json
     */
    public function isShow(): Json|bool|string
    {
        try {
            if($this->data['company_id'] < 1) return error($this->msg);
            if(!$this->settingIotModel->isShow($this->data)) throw new \Exception($this->settingIotModel->getMessage());
            return success($this->settingIotModel->getResult(),$this->settingIotModel->getMessage());
        } catch (\Exception $e) {
            return error($e->getMessage(),$e->getCode(),[
                'line' => $e->getLine(),
                'file' => $e->getFile()
            ]);
        }
    }

    /**
     * 获取功能室分组列表的所有数据ID
     * @return Json|bool|string
     */
    public function getAllData(): Json|bool|string
    {
        try {
            if($this->data['company_id'] < 1) return error($this->msg);
            if(!$this->settingIotModel->getAllDate($this->data)) throw new \Exception($this->settingIotModel->getMessage());
            return success($this->settingIotModel->getResult(),$this->settingIotModel->getMessage());
        } catch (\Exception $e) {
            return error($e->getMessage(),$e->getCode(),[
                'line' => $e->getLine(),
                'file' => $e->getFile()
            ]);
        }
    }

    /**
     * 获取功能室分组列表
     * @return bool|string|Json
     */
    public function getGroupingList(): Json|bool|string
    {
        try {
            if(!$this->settingIotModel->getGroupingList($this->data)) throw new \Exception($this->settingIotModel->getMessage());
            return success($this->settingIotModel->getResult(),$this->settingIotModel->getMessage());
        } catch (\Exception $e) {
            return error($e->getMessage(),$e->getCode(),[
                'line' => $e->getLine(),
                'file' => $e->getFile()
            ]);
        }
    }

    public function getGroupingListAll(): Json|bool|string
    {
        try {
            if(!$this->settingIotModel->getGroupingList($this->data)) throw new \Exception($this->settingIotModel->getMessage());
            return success($this->settingIotModel->getResult(),$this->settingIotModel->getMessage());
        } catch (\Exception $e) {
            return error($e->getMessage(),$e->getCode(),[
                'line' => $e->getLine(),
                'file' => $e->getFile()
            ]);
        }
    }

    /**
     * 删除功能室分组
     * @return bool|string|Json
     */
    public function delGrouping(): Json|bool|string
    {
        try {
            if($this->data['company_id'] < 1) return error($this->msg);
            if(!$this->settingIotModel->delGrouping($this->data)) throw new \Exception($this->settingIotModel->getMessage());
            return success($this->settingIotModel->getResult(),$this->settingIotModel->getMessage());
        } catch (\Exception $e) {
            return error($e->getMessage(),$e->getCode(),[
                'line' => $e->getLine(),
                'file' => $e->getFile()
            ]);
        }
    }

    /**
     * 编辑功能室
     * @return bool|string|Json
     */
    public function editGrouping(): Json|bool|string
    {
        try {
            if($this->data['company_id'] < 1) return error($this->msg);
            if(!$this->settingIotModel->editGrouping($this->data)) throw new \Exception($this->settingIotModel->getMessage());
            return success($this->settingIotModel->getResult(),$this->settingIotModel->getMessage());
        } catch (\Exception $e) {
            return error($e->getMessage(),$e->getCode(),[
                'line' => $e->getLine(),
                'file' => $e->getFile()
            ]);
        }
    }

    /**
     * 添加功能室
     * @return bool|string|Json
     */
    public function addFunctionRoom(): Json|bool|string
    {
        try {
            if($this->data['company_id'] < 1) return error($this->msg);
            if(!$this->settingIotModel->addFunctionRoom($this->data)) throw new \Exception($this->settingIotModel->getMessage());
            return success($this->settingIotModel->getResult(),$this->settingIotModel->getMessage());
        } catch (\Exception $e) {
            return error($e->getMessage(),$e->getCode(),[
                'line' => $e->getLine(),
                'file' => $e->getFile()
            ]);
        }
    }

    /**
     * 获取功能室分组列表
     * @return bool|string|Json
     */
    public function getFunctionRoomList(): Json|bool|string
    {
        try {
            if(!$this->settingIotModel->getFunctionRoomList($this->data)) throw new \Exception($this->settingIotModel->getMessage());
            return success($this->settingIotModel->getResult(),$this->settingIotModel->getMessage());
        } catch (\Exception $e) {
            return error($e->getMessage(),$e->getCode(),[
                'line' => $e->getLine(),
                'file' => $e->getFile()
            ]);
        }
    }

    /**
     * 编辑功能室分组
     * @return bool|string|Json
     */
    public function editFunctionRoom(): Json|bool|string
    {
        try {
            if($this->data['company_id'] < 1) return error($this->msg);
            if(!$this->settingIotModel->editFunctionRoom($this->data)) throw new \Exception($this->settingIotModel->getMessage());
            return success($this->settingIotModel->getResult(),$this->settingIotModel->getMessage());
        } catch (\Exception $e) {
            return error($e->getMessage(),$e->getCode(),[
                'line' => $e->getLine(),
                'file' => $e->getFile()
            ]);
        }
    }

    /**
     * 删除
     * @return bool|string|Json
     */
    public function delFunctionRoom(): Json|bool|string
    {
        try {
            if($this->data['company_id'] < 1) return error($this->msg);
            if(!$this->settingIotModel->delFunctionRoom($this->data)) throw new \Exception($this->settingIotModel->getMessage());
            return success($this->settingIotModel->getResult(),$this->settingIotModel->getMessage());
        } catch (\Exception $e) {
            return error($e->getMessage(),$e->getCode(),[
                'line' => $e->getLine(),
                'file' => $e->getFile()
            ]);
        }
    }
}